<?php

namespace HelloAdmin\Controller;

class OrderController extends CommonController {

    private $Orders;

    public function __construct() {
        parent::__construct();
        $this->Orders = D('Common/Orders');
    }

    public function index() {
        $post = $this->GetParam('ship_name,ship_mobile,status,pay_status');
        if (!empty($post)) {
            $where = $this->search($post);
        }


        $count = $this->Orders->GetCount($where);
        parent::backPage($count);
        $data = $this->Orders->GetDataByWhere($where, 'createtime DESC', $this->offset, $this->numPerPage, $fields = '');
        $this->assign('Count', $count);

        $this->assign('data', $data[0]);
        $this->display();
    }

    /**
     * 修改支付状态
     */
    public function edit() {
        $order_id = $this->GetParam('order_id');
        $data = $this->Orders->GetDataById($order_id, 'order_id,ship_name,pay_status,status,total_amount,member_id,status,mark_text');

        $this->assign('data', $data);
        $this->display();
    }

    public function doEdite() {
        if ($_POST['order_id'] > 0) {
            $post = $this->GetParam('mark_text,order_id,pay_status,status,old_pay_status');
            $data = array(
                'order_id' => $post['order_id'],
                'status' => $post['status'],
                'pay_status' => $post['pay_status'],
                'mark_text' => $post['mark_text'],
            );
            //var_dump($data);exit;
            if ($this->Orders->SaveData($data)) {
                //支付记录
                if($post['old_pay_status'] == 0 && $post['pay_status'] == 1){
                    
                }
                $this->success('修改成功');
                exit;
            }
        }
        $this->error('修改失败');
    }

    private function search($post) {
        //var_dump($post);
        $where = ' AND 1=1 ';
        if (!empty($post['ship_mobile'])) {
            $where .= "AND `ship_mobile` like '" . $post['ship_mobile'] . "%' ";
        }
        if (!empty($post['ship_name'])) {
            $where .= "AND `ship_name` like '" . $post['ship_name'] . "%' ";
        }
        if (!empty($post['order_id'])) {
            $where .= "AND `order_id` like '" . $post['order_id'] . "%' ";
        }
        if (!empty($post['pay_status'][0])) {
            $where .= "AND `pay_status` = '" . $post['pay_status'][0] . "' ";
            $this->assign('sex1', $post['pay_status'][0]);
        }
        if (!empty($post['pay_status'][1])) {
            $where .= "AND `pay_status` = '" . $post['pay_status'][1] . "' ";
            $this->assign('sex2', $post['pay_status'][1]);
        }
        if (!empty($post['status'])) {
            $where .= "AND `status` = '{" . $post['status'] . "}' ";
        }
        return $where;
    }

}
